package com.dianping.cat.configuration.web.url.transform;
import com.dianping.cat.configuration.web.url.IVisitor;
import com.dianping.cat.configuration.web.url.entity.Code;
import com.dianping.cat.configuration.web.url.entity.PatternItem;
import com.dianping.cat.configuration.web.url.entity.UrlPattern;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
/*  15 */     this.m_linker = new DefaultLinker(true);




    this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

  public static UrlPattern parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static UrlPattern parse(InputStream in) {
/*  28 */     DefaultNativeParser parser = new DefaultNativeParser(in);
    UrlPattern urlPattern = new UrlPattern();

    try {
      urlPattern.accept(parser);
/*  33 */     } catch (RuntimeException e) {
/*  34 */       if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  37 */         throw e;
      }
    }

/*  41 */     parser.m_linker.finish();
/*  42 */     return urlPattern;
  }



  public void visitCode(Code code) {
    byte tag;
/*  49 */     while ((tag = readTag()) != -1) {
/*  50 */       visitCodeChildren(code, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitCodeChildren(Code code, int _field, int _type) {
/*  55 */     switch (_field) {
      case 1:
/*  57 */         code.setId(Integer.valueOf(readInt()));
        break;
      case 2:
/*  60 */         code.setName(readString());
        break;
      case 3:
/*  63 */         code.setStatus(Integer.valueOf(readInt()));
        break;
    }
  }



  public void visitPatternItem(PatternItem patternItem) {
    byte tag;
/*  72 */     while ((tag = readTag()) != -1) {
/*  73 */       visitPatternItemChildren(patternItem, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitPatternItemChildren(PatternItem patternItem, int _field, int _type) {
    switch (_field) {
      case 1:
/*  80 */         patternItem.setGroup(readString());
        break;
      case 2:
/*  83 */         patternItem.setName(readString());
        break;
      case 3:
/*  86 */         patternItem.setPattern(readString());
        break;
      case 4:
/*  89 */         patternItem.setDomain(readString());
        break;
      case 5:
/*  92 */         patternItem.setId(readInt());
        break;
    }
  }



  public void visitUrlPattern(UrlPattern urlPattern) {
    byte tag;
/* 101 */     if ((tag = readTag()) != -4) {
/* 102 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/* 105 */     while ((tag = readTag()) != -1) {
/* 106 */       visitUrlPatternChildren(urlPattern, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitUrlPatternChildren(UrlPattern urlPattern, int _field, int _type) {
/* 111 */     switch (_field) {
      case 33:
/* 113 */         if (_type == 1) {
/* 114 */           PatternItem patternItem = new PatternItem();

/* 116 */           visitPatternItem(patternItem);
/* 117 */           this.m_linker.onPatternItem(urlPattern, patternItem); break;
/* 118 */         }  if (_type == 2) {
/* 119 */           for (int i = readInt(); i > 0; i--) {
/* 120 */             PatternItem patternItem = new PatternItem();

/* 122 */             visitPatternItem(patternItem);
/* 123 */             this.m_linker.onPatternItem(urlPattern, patternItem);
          }
        }
        break;
      case 34:
/* 128 */         if (_type == 1) {
/* 129 */           Code code = new Code();

/* 131 */           visitCode(code);
/* 132 */           this.m_linker.onCode(urlPattern, code); break;
/* 133 */         }  if (_type == 2) {
/* 134 */           for (int i = readInt(); i > 0; i--) {
/* 135 */             Code code = new Code();

/* 137 */             visitCode(code);
/* 138 */             this.m_linker.onCode(urlPattern, code);
          }
        }
        break;
    }
  }

  private int readInt() {
    try {
/* 147 */       return (int)readVarint(32);
/* 148 */     } catch (IOException e) {
/* 149 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 155 */       return this.m_in.readUTF();
/* 156 */     } catch (IOException e) {
/* 157 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 163 */       return this.m_in.readByte();
/* 164 */     } catch (IOException e) {
/* 165 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 170 */     int shift = 0;
/* 171 */     long result = 0L;

/* 173 */     while (shift < length) {
/* 174 */       byte b = this.m_in.readByte();
/* 175 */       result |= (b & 0x7F) << shift;
/* 176 */       if ((b & 0x80) == 0) {
/* 177 */         return result;
      }
/* 179 */       shift += 7;
    }

/* 182 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-core-3.0.0.jar!/com/dianping/cat/configuration/web/url/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */